import {
  getOpenid
} from '../../api/index'
Page({
  data: {
    loading: true,
    userInfo: null
  },
  onLoad: function (options) {
    const openid = wx.getStorageSync('openid')
    const userInfo = wx.getStorageSync('userinfo')
    if (openid && userInfo) {
      this.setData({
        userInfo,
        loading: false
      })
    } else {
      this.setData({
        loading: false
      })
    }
  },
  getUserProfile(data) {
    wx.getUserProfile({
      lang: "zh_CN",
      desc: "用于完善会员资料",
      success: data => {
        wx.showLoading({
          title: '登陆中',
          mask: true
        })
        getOpenid(data.userInfo).then(res => {
          wx.setStorageSync('openid', res.openid)
          wx.setStorageSync('userinfo', data.userInfo)
          this.setData({
            userInfo: data.userInfo
          })
          wx.hideLoading()
          wx.lin.showMessage({
            type: 'success',
            content: '登录成功'
          })
        })
      },
      fail() {
        wx.showToast({
          icon: 'none',
          title: '已取消登录'
        })
      }
    })
  },

  logout() {
    wx.removeStorageSync('openid')
    this.setData({
      userInfo: ''
    })
    wx.lin.showMessage({
      type: 'success',
      content: '退出成功'
    })
  },
})